package screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.VisibleAction;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.Scaling;
import com.me.jnrgame.JnR;
import static com.badlogic.gdx.scenes.scene2d.actions.Actions.delay;
import static com.badlogic.gdx.scenes.scene2d.actions.Actions.fadeIn;
import static com.badlogic.gdx.scenes.scene2d.actions.Actions.fadeOut;
import static com.badlogic.gdx.scenes.scene2d.actions.Actions.sequence;

public class SplashScreen implements Screen {

	private Stage stage;
	private Image splashImage;
	private JnR game;
	private LevelScreen levelScreen;

	public SplashScreen(JnR _game) {
		game = _game;
		stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(),
				true);

	}

	@Override
	public void show() {
		float delta = Gdx.graphics.getDeltaTime();
		Gdx.input.setInputProcessor(stage);

		// start playing the menu music

		// retrieve the splash image's region from the atlas
		FileHandle fileHandle = Gdx.files.classpath("data/world/packs");
		Texture texture = new Texture(Gdx.files.internal("data/splash.png"));

		splashImage = new Image(texture);
		splashImage.setScaling(Scaling.stretch);
		splashImage.setFillParent(true);

		// this is needed for the fade-in effect to work correctly; we're just
		// making the image completely transparent

		// configure the fade-in/out effect on the splash image
		splashImage.addAction(sequence(fadeIn(0.75f), delay(1.75f),
				fadeOut(0.75f), new Action() {
					public boolean act(float delta) {
						game.toMainMenuScreen();
						return true; // returning true consumes the event
					}
				}));

		// and finally we add the actor to the stage
		stage.addActor(splashImage);
	}

	@Override
	public void render(float delta) {
		// TODO Auto-generated method stub
		stage.act(delta);

		// (2) draw the result

		// clear the screen with the given RGB color (black)
		Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

		// draw the actors
		stage.draw();
		if (Gdx.input.justTouched() || Gdx.input.isTouched()) {
			game.toMainMenuScreen();
		}

	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}
